package com.feizi.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.feizi.entity.PdTypeEntity;
import com.feizi.entity.Product;
import com.feizi.entity.ProductVO;
import com.feizi.service.ProductService;

@Controller
public class ProductController {
	@Autowired
    private ProductService productService;
	/**
	 * 查询产品
	 * @param productVO
	 * @return
	 */
	
	@RequestMapping(path = "/findProduct.do",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findProduct(String typeId,ProductVO productVO) {
		if(typeId!="" && typeId!=null) {
			productVO.setF_ptId(Integer.parseInt(typeId));
		}
		//设置mysql分页用的偏移量
		productVO.setOffset((productVO.getPage() - 1) * productVO.getLimit());
		//查询条数
		int count = productService.getCount(productVO);
		//调用查询
		List<Product> list = productService.find(productVO);
		System.out.println(list.size());
		//返回结果
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", "0");
		rslMap.put("msg", "success");
		rslMap.put("count", count);
		rslMap.put("data", list);
		
		return rslMap;
	}
	/**
	 * 查询产品分类
	 * @return
	 */
	
	@RequestMapping(path = "/findPdType.do",method = RequestMethod.GET)
	@ResponseBody
	public List<PdTypeEntity> findPdType() {
		//调用查询
		List<PdTypeEntity> list = productService.findType();
		
		
		return list;
	}
	/**
	 * 新增产品
	 * @param Product
	 * @return
	 */
	@RequestMapping(path = "/addProduct.do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addProduct(Product product) {
		//调用新增
		int rows = productService.addProduct(product);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "新增成功" : "新增失败");
		
		return rslMap;
	}
	
	/**
	 * 删除产品
	 * @param Product
	 * @return
	 */
	@RequestMapping(path = "/deleteProduct.do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteProduct(Integer id) {
		//调用新增
		int rows = productService.deleteProduct(id);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "删除成功" : "删除失败");
		
		return rslMap;
	}
	/**
	 * 修改产品
	 * @param Product
	 * @return
	 */
	@RequestMapping(path = "/editProduct.do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editProduct(Product product) {
		//调用新增
		int rows = productService.editProduct(product);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "修改成功" : "修改失败");
		
		return rslMap;
	}
	/**
	 * 修改图片
	 * @param Product
	 * @return
	 */
	@RequestMapping(path="/uploadImg.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> uploadImg(@RequestParam("file") MultipartFile fileUpload) throws Exception{
        if(fileUpload!=null){
            
        	// 下面是图片上传的代码
            // 得到图片的原始文件名
            String originalName = fileUpload.getOriginalFilename();
            // 指定带盘符的路径, 物理路径
            String realPath = "D://upload//finalitem//images//";
            // 通过UUID算法生成新的文件名
            String uuidName = UUID.randomUUID().toString();
            // UUID名称加上文件的后缀名
            String newFile = uuidName + originalName.substring(originalName.lastIndexOf("."));
            // 创建File文件
            File file = new File(realPath + newFile);
            // 将图片写入到具体的位置
            fileUpload.transferTo(file);
        	
            Map<String,Object> map=new HashMap<>();
            map.put("code",0);
            map.put("msg","");
            map.put("data","img/" + newFile);
            
            return map;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        return map;
	}
	/**
	 * 是否上架按钮修改
	 * @param product
	 * @return
	 */
	@RequestMapping(path="/upputaway.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> upputaway(String voide,Product product){
		product.setPutaway("on".equals(voide)?1:0);
		int count=productService.getOrder(product.getId());
		int rows=0;
		if(count==0) {
			rows = productService.upputaway(product);
		}
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "修改成功" : "修改失败");
		
		return rslMap;
	};
	/**
	 * 是否删除按钮修改
	 * @param product
	 * @return
	 */
	@RequestMapping(path="/upisDel.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> upisDel(String voide,Product product){
		product.setIsDel("on".equals(voide)?1:0);
		int rows = productService.upisDel(product);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "修改成功" : "修改失败");
		
		return rslMap;
	};
	/**
	 * 是否推荐按钮修改
	 * @param product
	 * @return
	 */
	@RequestMapping(path="/uprecommend.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> uprecommend(String voide,Product product){
		product.setRecommend("on".equals(voide)?1:0);
		int rows = productService.uprecommend(product);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? "修改成功" : "修改失败");
		
		return rslMap;
	};
	@RequestMapping(path="/updateProductImg.do", method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateProductImg(Product product){
        
		//调用新增
		int rows = productService.updateProductImg(product);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "0" : "1");
		rslMap.put("msg", rows>0? " 修改成功" : "修改失败");
		
		return rslMap;
		
		
	}
	
}
		
		